package com.zay.a5_buttons;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import com.zay.a5_buttons.adapter.BookAdapter;
import com.zay.a5_buttons.pojo.Book;
import java.util.ArrayList;

public class StudentSystemActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_student_system);
        ListView lv_main=findViewById(R.id.lv_main);
        Button btn_insert=findViewById(R.id.btn_insert);
        // 先拿到数据并放在适配器上
        ArrayList<Book> bookList=getAllBooks();
        BookAdapter bookAdapter=new BookAdapter(StudentSystemActivity.this,R.layout.book_item,
                bookList);
        // 将适配器上的数据传递给listView
        lv_main.setAdapter(bookAdapter);
        // 为ListView注册一个监听器，当用户点击了ListView中的任何一个子项时，就会回调onItemClick()方法
        // 在这个方法中可以通过position参数判断出用户点击的是那一个子项
        lv_main.setOnItemClickListener((parent, view, position, id) -> {
            Book book=bookList.get(position);
            Toast.makeText(StudentSystemActivity.this,book.getBook_name(),Toast.LENGTH_SHORT).show();
        });
        btn_insert.setOnClickListener(view -> {
            AlertDialog.Builder builder = new AlertDialog.Builder(StudentSystemActivity.this);
            view = View.inflate(StudentSystemActivity.this, R.layout.custom_dialog, null);
            builder.setView(view);
            builder.setCancelable(true);
            EditText book_name_et=view.findViewById(R.id.add_book_name);
            EditText book_publish_et= view.findViewById(R.id.add_book_publish);
            EditText book_price_et=view.findViewById(R.id.add_book_price);
            //取消按钮
            Button btn_cancel=view.findViewById(R.id.add_btn_cancel);
            //确定按钮
            Button btn_comfirm=view.findViewById(R.id.add_btn_comfirm);
            //取消或确定按钮监听事件处理
            btn_comfirm.setOnClickListener(view1 -> {
                try{
                    String book_name=book_name_et.getText().toString();
                    String book_publish=book_publish_et.getText().toString();
                    double book_price=Double.parseDouble(book_price_et.getText().toString());
                    Book book=new Book(book_name,book_publish,book_price);
                    long row = insertBook(book);
                    Toast.makeText(StudentSystemActivity.this,"插入成功"+row+"行数据",Toast.LENGTH_SHORT).show();
                }catch(NumberFormatException e){
                    Toast.makeText(StudentSystemActivity.this,"输入格式有误",Toast.LENGTH_SHORT).show();
                }
            });
            AlertDialog dialog = builder.create();
            dialog.show();
            /*AlertDialog.Builder builder=new AlertDialog.Builder(StudentSystemActivity.this);
            builder.setTitle("插入书籍");
            builder.setMessage("内容");
            builder.setPositiveButton("插入", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    Toast.makeText(StudentSystemActivity.this,"插入成功",Toast.LENGTH_SHORT).show();
                }
            });
            AlertDialog dialog=builder.create();
            dialog.show();*/
        });
    }
    @SuppressLint("Range")
    public ArrayList<Book> getAllBooks(){
        ArrayList<Book> bookLit=new ArrayList<>();
        // 创建DatabaseHelper对象
        MySQLiteOpenHelper dbHelper = new MySQLiteOpenHelper(StudentSystemActivity.this,"book_info",null,1);
        // 调用getWritableDatabase()方法创建或打开一个可以读的数据库
        SQLiteDatabase sqLiteDatabase = dbHelper.getReadableDatabase();
        //游标,为查询数据库返回的结果
        Cursor cursor=sqLiteDatabase.query("book_info",null,null,null,null,
                null,null);
        while (cursor.moveToNext()){
            Book book=new Book();
            book.setBook_id(cursor.getInt(cursor.getColumnIndex("book_id")));
            book.setBook_name(cursor.getString(cursor.getColumnIndex("book_name")));
            book.setBook_publish(cursor.getString(cursor.getColumnIndex("book_publish")));
            book.setBook_pirce(cursor.getDouble(cursor.getColumnIndex("book_price")));
            bookLit.add(book);
        }
        //断开游标连接
        cursor.close();
        //断开数据库链接
        sqLiteDatabase.close();
        return bookLit;
    }
    public long insertBook(Book book){
        // 创建DatabaseHelper对象
        MySQLiteOpenHelper dbHelper = new MySQLiteOpenHelper(StudentSystemActivity.this,"book_info",null,1);
        // 调用getWritableDatabase()方法创建或打开一个可以读的数据库
        SQLiteDatabase sqLiteDatabase = dbHelper.getReadableDatabase();
        ContentValues values=new ContentValues();
        values.put("book_name",book.getBook_name());
        values.put("book_publish",book.getBook_publish());
        values.put("book_price",book.getBook_pirce());
        long row = sqLiteDatabase.insert("book_info",null,values);
        sqLiteDatabase.close();
        return row;
    }
}